package br.com.sgi.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.hibernate.Transaction;

import br.com.sgi.util.HibernateUtil;

public class HibernateFilter implements Filter {

	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		Transaction transaction = HibernateUtil.getSession().beginTransaction();
		
		try {
			System.out.println("HibernateFilter.doFilter ini");
			chain.doFilter(request, response);
			System.out.println("HibernateFilter.doFilter fim");
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	@Override
	public void init(FilterConfig config) throws ServletException {}
}
